package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.NewAddressTask;
import com.xgkj.diyiketang.util.ToastUtil;

/**
 * Created by XCKJ_Paladowow on 2016/8/20.
 */
public class NewAddrActivity extends BaseActivity {
    private Context mContext;
    private EditText mNameEdit;
    private EditText mPhoneEdit;
    private EditText mAddrEdit;
    private static final int PHONE_LENGTH = 11;
    private static final String PHONE_REGEX = "1[34578]\\d{9}";
    private Button sureBtn;
    private TextView mback;

    private String nameString;
    private String phoneString;
    private String addrString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_address);
        mContext = this;
        initView();
    }

    private void initView() {
        mNameEdit = (EditText) findViewById(R.id.name_edit);
        mPhoneEdit = (EditText) findViewById(R.id.phone_edit);
        mAddrEdit = (EditText) findViewById(R.id.addr_edit);
        sureBtn = (Button) findViewById(R.id.sure_btn);
        mback = (TextView) findViewById(R.id.back);

        mback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        sureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkInput()) {
                    doCreate();
                }

            }
        });
    }

    private void doCreate() {
        NewAddressTask task = new NewAddressTask(nameString, addrString, phoneString);
        task.addListener(createTaskListner);
        getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener createTaskListner = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            ToastUtil.showMessage(mContext, "新建收货地址成功");
            finish();
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    private boolean checkInput() {
        nameString = mNameEdit.getText().toString();
        phoneString = mPhoneEdit.getText().toString();
        addrString = mAddrEdit.getText().toString();
        if (TextUtils.isEmpty(nameString)) {
            Toast.makeText(mContext, "请先输入收货人真实姓名", Toast.LENGTH_SHORT)
                    .show();
            return false;
        }else if (TextUtils.isEmpty(phoneString) || phoneString.length() != PHONE_LENGTH
                || !phoneString.matches(PHONE_REGEX)) {
            Toast.makeText(mContext, R.string.no_phone, Toast.LENGTH_SHORT)
                    .show();
            return false;
        } else if (TextUtils.isEmpty(addrString)) {
            Toast.makeText(mContext, "请先输入收货人真实收货地址", Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
        return true;
    }

}
